package cn.qiyu5522.service.impl;

import cn.qiyu5522.domain.entity.ArticleTag;
import cn.qiyu5522.mapper.ArticleTagMapper;
import cn.qiyu5522.service.ArticleTagService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 文章标签关联表 服务实现类
 * </p>
 *
 * @author Tonkey
 * @since 2024-04-24
 */
@Service
public class ArticleTagServiceImpl extends ServiceImpl<ArticleTagMapper, ArticleTag> implements ArticleTagService {
    @Override
    public List<Long> getTagIdsByArticleId(Long id) {
        LambdaQueryWrapper<ArticleTag> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ArticleTag::getArticleId,id);
        List<ArticleTag> list = list(queryWrapper);
        // 过滤出 文章id 对应的 标签id
        List<Long> tags = list.stream().map(at -> at.getTagId()).collect(Collectors.toList());
        return tags;
    }

    @Override
    public void removeByArticleId(Long id) {
        LambdaQueryWrapper<ArticleTag> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ArticleTag::getArticleId,id);
        remove(queryWrapper);
    }

    @Override
    public void saveBatchByArticleTagList(Long articleId, List<Long> tags) {
        List<ArticleTag> articleTags = tags.stream()
                .map(tagId -> new ArticleTag(articleId, tagId))
                .collect(Collectors.toList());
        saveBatch(articleTags);
    }

}
